import { _decorator, Component, director, Label, Sprite, tween, Tween } from "cc";
import AssetMgr from "./AssetMgr";
import { objPool } from "../game/fish/objPool";

const {ccclass, property , menu} = _decorator;

@ccclass
@menu("use/loadGame")
export default class LoadGame extends Component {

    @property(Sprite)
    loadSpr: Sprite = null;

    @property(Label)
    lb_loading: Label = null;

    curProgress = 0;
    loadEnd = false;

    // LIFE-CYCLE CALLBACKS:

    async onLoad () {
        await this.loadGameAssets();

        console.log('...')

        let prefab = await AssetMgr.Inst.getOnePrefab("prefabs/game/battery" , "gameBundle");
        objPool.ins.init("battery" , prefab ,100)

        if(this.loadEnd){
            director.loadScene('home')
            console.log("加载完成！");
        }
    }

    start () {

    }

    
    /**
     * 开始加载资源
     */
    async loadGameAssets() {
        this.loadSpr.fillRange = 0;

        let assMgr = AssetMgr.Inst;
        let loadAssetsAry: { bundleName, assetType, loadingText, completeFun, assetPath?}[] = [
            { bundleName: "homeBundle", assetType: "bundle", loadingText: "load ass...", 
                completeFun: (bundle) => { assMgr.addOneBundle("homeBundle", bundle); } },
            { bundleName: "gameBundle", assetType: "bundle", loadingText: "load game...", 
                completeFun: (bundle) => { assMgr.addOneBundle("gameBundle", bundle); } },
        ];

        let oneProgressRate: number = 1 / loadAssetsAry.length;
        this.addProgress(0.3,1);//0.2 加点进度条初始动画
        for (let i = 0; i < loadAssetsAry.length; i++) {
            let loadAssets = loadAssetsAry[i];
            let bundleName = loadAssets.bundleName;
            let assetType = loadAssets.assetType;
            let completeFun = loadAssets.completeFun;
            let assetPath = loadAssets.assetPath;
            let loadingText = loadAssets.loadingText;
            this.setLoadingText(loadingText);
            await new Promise<void>(resolve => {
                switch (assetType) {
                    case "bundle": {
                        assMgr.getOneBundle(bundleName).then((bundle) => {
                            completeFun && completeFun(bundle);
                            this.addProgress(oneProgressRate);
                            resolve();
                        });
                        break;
                    }
                    case "scene": {
                        director.preloadScene(assetPath, (completedCount, totalCount) => {
                            let ratio = (1 - (completedCount / totalCount)) * oneProgressRate;
                            this.addProgress(ratio);
                        }, error => {
                            resolve();
                        });
                        break;
                    }
                }
            });
        }
        this.setLoadingText("加载完成");
        await this.addProgress(oneProgressRate);
        if (this.curProgress >= 1) {
            this.loadEnd = true;
            console.log('加载完成')
        }
    }

    setLoadingText(text: string) {
        this.lb_loading.string = text;
    }

    addProgress(ratio: number,_time:number = 0.5) {
        return new Promise<void>(resolve => {
            this.curProgress += ratio;
            this.curProgress = Math.min(this.curProgress, 1);
            Tween.stopAllByTarget(this.loadSpr);
            tween(this.loadSpr).to(_time, { fillRange: this.curProgress }).call(() => {
                resolve();
            }).start();
        });
    }

    // update (dt) {}
}
